package com.njworkorder.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.SafetyEducationAddDto;
import com.njworkorder.DTO.SecurityCheckAddDto;
import com.njworkorder.Entity.*;
import com.njworkorder.Service.SafetyEducationService;
import com.njworkorder.Service.SecurityCheckService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.SafetyEducationVo;
import com.njworkorder.VO.SecurityCheckVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "安全管理")
@RestController
@RequestMapping("/SafetyM")
public class SafetyManagementController {

    private final SafetyEducationService safetyEducationService;
    private final SecurityCheckService securityCheckService;
    public  SafetyManagementController(SafetyEducationService safetyEducationService,SecurityCheckService securityCheckService){
        this.safetyEducationService = safetyEducationService;
        this.securityCheckService = securityCheckService;
    }

    @Operation(summary = "安全教育信息添加")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@Valid @RequestBody SafetyEducationAddDto safetyEducationAddDto) {
        SafetyEducation safetyEducation = new SafetyEducation();
        BeanUtils.copyProperties(safetyEducationAddDto, safetyEducation);

        return safetyEducationService.save(safetyEducation) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @Operation(summary = "根据ID删除安全教育信息")
    @GetMapping("/deleteInfo")
    public Result<String> deleteInfo(@RequestParam("id") String id) {
        return safetyEducationService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }


    @Operation(summary = "根据id更新安全教育信息")
    @PostMapping("/updateInfo/{id}")
    public Result<String> updateInfo(@RequestBody SafetyEducationAddDto safetyEducationAddDto,@PathVariable("id") String id) {
        SafetyEducation safetyEducation = new SafetyEducation();
        BeanUtils.copyProperties(safetyEducationAddDto, safetyEducation);
        safetyEducation.setId(id);
        return safetyEducationService.updateById(safetyEducation) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @Operation(summary = "根据ID查询安全教育信息")
    @GetMapping("/getInfoById")
    public Result<SafetyEducationVo> getInfoById(@RequestParam("id") String id) {
        MPJLambdaWrapper<SafetyEducation> wrapper = new MPJLambdaWrapper<SafetyEducation>()
                .selectAll(SafetyEducation.class)
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId,SafetyEducation::getAoId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName, SafetyEducationVo::getAoName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,SafetyEducation::getTrainingApId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName, SafetyEducationVo::getTrainingApName))
                .eq(SafetyEducation::getId, id);

        SafetyEducationVo safetyEducationVo = safetyEducationService.selectJoinOne(SafetyEducationVo.class, wrapper);
        return ResultUtil.success(safetyEducationVo);
    }

    @Operation(summary = "获取安全教育信息列表")
    @GetMapping("/getList")
    public Result<Page<SafetyEducationVo>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize,
                                                   @RequestParam(value = "trainingTheme",required = false) String trainingTheme,
                                                   @RequestParam(value = "trainingDate",required = false) String trainingDate){
        MPJLambdaWrapper<SafetyEducation> wrapper = new MPJLambdaWrapper<SafetyEducation>()
                .selectAll(SafetyEducation.class)
                .leftJoin(AdministrativeOrganization.class,AdministrativeOrganization::getId,SafetyEducation::getAoId,ext ->
                        ext.selectAs(AdministrativeOrganization::getName, SafetyEducationVo::getAoName))
                .leftJoin(AdministrativePersonnel.class,AdministrativePersonnel::getId,SafetyEducation::getTrainingApId,ext ->
                        ext.selectAs(AdministrativePersonnel::getName, SafetyEducationVo::getTrainingApName))
                .apply("1=1")
                .like(StringUtils.isNotEmpty(trainingTheme),SafetyEducation::getTrainingTheme,trainingTheme)
                .likeRight(StringUtils.isNotEmpty(trainingDate),SafetyEducation::getTrainingDate,trainingDate);
        Page<SafetyEducationVo> safetyEducationVoPage = safetyEducationService.selectJoinListPage(new Page<>(pageIndex, pageSize), SafetyEducationVo.class, wrapper);
        return ResultUtil.success(safetyEducationVoPage);
    }


    @Operation(summary = "添加安全检查信息")
    @PostMapping("/addSecurityCheckInfo")
    public Result<String> addSecurityCheckInfo(@RequestBody SecurityCheckAddDto securityCheckAddDto,@RequestHeader("Create-Ap-Id") String createApId){

        SecurityCheck securityCheck = new SecurityCheck();
        BeanUtils.copyProperties(securityCheckAddDto, securityCheck);
        securityCheck.setCreateApId(createApId);

        return securityCheckService.save(securityCheck) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @Operation(summary = "根据id更新安全检查信息")
    @PutMapping("/updateSecurityCheckInfoById/{id}")
    public Result<String> updateSecurityCheckInfoById(@RequestBody SecurityCheckAddDto securityCheckAddDto,@PathVariable("id") String id){

        SecurityCheck securityCheck = new SecurityCheck();
        BeanUtils.copyProperties(securityCheckAddDto, securityCheck);
        securityCheck.setId(id);

        return securityCheckService.updateById(securityCheck) ? ResultUtil.success("更新完成") : ResultUtil.fail("更新失败");
    }

    @Operation(summary = "根据id删除安全检查信息")
    @GetMapping("/deleteSecurityCheckInfoById")
    public Result<String> deleteSecurityCheckInfoById(@RequestParam("id") String id) {
        return securityCheckService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }


    @Operation(summary = "根据id获取安全检查信息详情")
    @GetMapping("/getSecurityCheckDetailsById")
    public Result<SecurityCheckVo> getSecurityCheckDetailsById(@RequestParam("id") String id) {

        MPJLambdaWrapper<SecurityCheck> wrapper = securityCheckService.getMPJWrapper()
                .eq(SecurityCheck::getId, id);

        SecurityCheckVo securityCheckVo = securityCheckService.selectJoinOne(SecurityCheckVo.class, wrapper);
        return ResultUtil.success(securityCheckVo);
    }


    @Operation(summary = "获取安全检查信息列表")
    @GetMapping("/getSecurityCheckList")
    public Result<Page<SecurityCheckVo>> getSecurityCheckList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){

        MPJLambdaWrapper<SecurityCheck> wrapper = securityCheckService.getMPJWrapper()
                .leftJoin(Plan.class,Plan::getId,SecurityCheck::getPlanId,ext ->
                        ext.selectAs(Plan::getPlanName,SecurityCheckVo::getPlanName))
                .apply("1=1");

        Page<SecurityCheckVo> securityCheckVoPage = securityCheckService.selectJoinListPage(new Page<>(pageIndex, pageSize), SecurityCheckVo.class, wrapper);
        return ResultUtil.success(securityCheckVoPage);
    }





}
